package com.kugou.miniapp.service;

import com.kugou.miniapp.dao.MusicMapper;
import com.kugou.miniapp.domain.Music;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MusicService {
    @Autowired
    MusicMapper musicMapper;

    public List<String> getHisMusic(String user_id) {
        return musicMapper.getHisMusicByUserId(user_id);
    }

    public List<String> getLikeByUserId(String user_id){

        return musicMapper.getLikeByUserId(user_id);
    }

    public int like(String user_id, String music_id){
        Music music = new Music();
        music.setUser_id(user_id);
        music.setMusic_id(music_id);
        return musicMapper.insertLikeMusic(music);
    }

    public boolean musicIsMyLike(String user_id, String music_id){
        List<Music> musicList = musicMapper.getLikeMusicByUserId(user_id);
        for (Music music: musicList){
            if (music.getMusic_id().equals(music_id)){
                return true;
            }
        }
        return false;
    }

    //添加历史播放音乐
    public int addHisMusic(String user_id, String music_id, String music_name){
        Music music = new Music();
        music.setMusic_id(music_id);
        music.setUser_id(user_id);
        music.setDate(new Date().getTime());
        music.setMusic_name(music_name);
        //查看数据库是否有该记录
        Music m = musicMapper.getHisMusicByMIdAndUId(music);
        //若有则更新， 无则插入
        if (m != null){
            //更新
            return musicMapper.updateHisMusic(music);
        }else {
            //插入
            return musicMapper.insertHisMusic(music);
        }

    }


    //查询用户所有音乐
    public List<Music> getAllMusicByUserId(String userid){
        List<Music> musicList = musicMapper.getMusicsByUserId(userid);
        return musicList;
    }


    public int disLike(String user_id, String music_id) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("user_id", user_id);
        condition.put("music_id", music_id);
        return musicMapper.delLikeMusicByUserIdAndMusicId(condition);
    }

    public int delHis(String user_id, String music_id) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("user_id", user_id);
        condition.put("music_id", music_id);
        return musicMapper.delHisMusicByUserIdAndMusicId(condition);
    }
}
